using System;
using System.Collections;
using System.IO;
using System.Text;
using UnityEngine;

namespace PKGames.AssetLoader
{
	public class AssetRequestManager : MonoSingleton<AssetRequestManager>
	{
		[SerializeField]
		private AssetQueue _downloadContentQueue;

		[SerializeField]
		private AssetQueue _downloadQueue;

		[SerializeField]
		private AssetQueue _readQueue;

		[SerializeField]
		private AssetQueue _magicCameraAssetBundleQueue;

		public AssetQueue DownloadContentQueue => _downloadContentQueue;

		public AssetQueue DownloadQueue => _downloadQueue;

		public AssetQueue ReadQueue => _readQueue;

		public AssetQueue MagicCameraAssetBundleQueue => _magicCameraAssetBundleQueue;

		protected override void Awake()
		{
			base.Awake();
			_downloadContentQueue.Setup(this);
			_downloadQueue.Setup(this);
			_readQueue.Setup(this);
			_magicCameraAssetBundleQueue.Setup(this);
		}

		protected override void OnDestroy()
		{
			base.OnDestroy();
		}

		private void Update()
		{
			_downloadContentQueue.Update();
			_downloadQueue.Update();
			_readQueue.Update();
			_magicCameraAssetBundleQueue.Update();
		}

		public string ReadTextFile(string localFilePath)
		{
			UnityEngine.Debug.Log(base.Tag + "Reading local file: " + localFilePath);
			try
			{
				return File.ReadAllText(localFilePath, Encoding.UTF8);
			}
			catch (Exception ex)
			{
				UnityEngine.Debug.LogWarning(base.Tag + "Could not read the text file: " + localFilePath + "\n" + ex.ToString());
			}
			return string.Empty;
		}

		public bool WriteTextFile(string localFilePath, string text)
		{
			UnityEngine.Debug.Log(base.Tag + "Writting local file: " + localFilePath);
			try
			{
				File.WriteAllText(localFilePath, text, Encoding.UTF8);
				FileHelper.DoNotBackupInCloud(localFilePath);
				return true;
			}
			catch (Exception ex)
			{
				UnityEngine.Debug.LogWarning(base.Tag + "Could not write the text file: " + localFilePath + "\n" + ex.ToString());
			}
			return false;
		}

		public IEnumerator DownloadTextFile(string url, Action<string> OnSuccess, Action OnFailure = null)
		{
			UnityEngine.Debug.Log(base.Tag + "Starting download :" + url);
			HTTPDownloader downloader = new HTTPDownloader(url);
			yield return StartCoroutine(downloader.Download(null, 1f));
			if (downloader.DownloadState == AssetRequest.State.Failed)
			{
				UnityEngine.Debug.LogWarning(base.Tag + "Download error. Could not download: " + url);
			}
			else
			{
				try
				{
					string @string = Encoding.UTF8.GetString(downloader.Bytes);
					OnSuccess.Fire(@string);
				}
				catch (Exception ex)
				{
					UnityEngine.Debug.LogWarning(base.Tag + "Could not convert downloaded file to text: " + url + "\n" + ex.ToString());
				}
			}
		}

		private IEnumerator InternalWaitFor(float seconds, Action onComplete)
		{
			yield return new WaitForSeconds(seconds);
			onComplete.Fire();
		}

		public void WaitFor(float seconds, Action onComplete)
		{
			StartCoroutine(InternalWaitFor(seconds, onComplete));
		}

		public void AddToMagicCameraQueue(DownloadRequest request)
		{
			DownloadRequest request2 = _magicCameraAssetBundleQueue.GetRequest<DownloadRequest>(request.Id);
			if (request2 == null)
			{
				_magicCameraAssetBundleQueue.Add(request);
			}
			else
			{
				request2.AddListerners(request);
			}
		}
	}
}
